<template>
  <page-wrap back title="简历诊断" hideTab stayStill :hideSafeArea="true">
    <viwe class="resume">
      <image class="diagnosis-img" mode="widthFix" :src="img1"></image>
      <image class="diagnosis-img" mode="widthFix" :src="img2"></image>
      <image class="diagnosis-img" mode="widthFix" :src="img3"></image>
      <image class="diagnosis-img" mode="widthFix" :src="img4"></image>
      <!-- <image class="diagnosis-img" mode="widthFix" :src="img5"></image> -->
      <!-- <view  class="u-text-center,link">
        <u-link  href="http://www.baidu.com" color="white">活动详情查看《简历诊断服务协议》</u-link>
      </view> -->
      <template v-if="personId">
        <!-- <image v-if="personId" :src="isPay? consultingImg: experienceImg" class="btn-confirm" @click="handleSendClick"></image> -->
        <image
          v-if="personId"
          :src="experienceImg"
          class="btn-confirm"
          @click="handleSendClick"
        ></image>
      </template>
      <template v-else-if="isAuthSetting">
        <u-button
          class="lm-wechat"
          :hair-line="false"
          hover-class="none"
          @click="showLoginDialog"
        >
          <image :src="experienceImg" class="btn-experience"></image>
        </u-button>
      </template>
      <template v-else>
        <u-button
          class="lm-wechat"
          open-type="getUserInfo"
          :hair-line="false"
          hover-class="none"
          @getuserinfo="getUserInfo"
          :loading="getUserInfoLoading"
        >
          <image :src="experienceImg" class="btn-experience"></image>
        </u-button>
      </template>

      <LoginDialog
        ref="loginDialog"
        @loginSuccessBack="loginSuccessBack"
      ></LoginDialog>
    </viwe>
  </page-wrap>
</template>

<script>
import personDetailMixin from "../../company/mixins/personDetail.js";
import WxLoginMixin from "@/mixins/wxLogin";
import IMEventHandler from "@/utils/im";
import ShareMixin from "@/mixins/share";
import LoginDialog from "@/components/LoginDialog";
import { isPay, addOrderAndPay } from "@/api/modules/activity";
// import { getResumeFuJianDiagnoseStatus } from "@/api/index";
import { getPageResumeDiagnoseRecord, getLeftUnlockNum } from "@/api/index";

export default {
  components: {
    LoginDialog,
  },
  mixins: [personDetailMixin, WxLoginMixin, ShareMixin],
  data() {
    return {
      personId: "",
      sendImg: "",
      chatId: "24371169", // 顾问管家id："24371169"（手机号：18028282828)
      selfInfo: getApp().globalData.imInfo,
      isPay: false,
      provider: "",
      paying: false,
      isCheck: false, // 是否已经获取过是否支付状态
      price: 0, // 单位分，广告用
      priceText: 199, // 价格
      originalPrice: 399, // 原价
      img1: this.$imgBaseSelf + "career/diagnosis/1.png",
      img2: this.$imgBaseSelf + "career/diagnosis/2.png",
      img3: this.$imgBaseSelf + "career/diagnosis/3.png",
      img4: this.$imgBaseSelf + "career/diagnosis/4.png",
      experienceImg: this.$imgBaseSelf + "career/diagnosis/experience.png",
      consultingImg: this.$imgBaseSelf + "career/diagnosis/consulting.png",
      shareTitle: this.$selfName + " - 简历诊断",
      sharePath: "/pagesSub/career/activity/ResumeDiagnosis?fromWhere=careerPath",
      fromWhere: "discovery",
      resumeList: [],
    };
  },
  onLoad(options) {
    this.fromWhere = options.fromWhere || "discovery";
    this.personId = uni.getStorageSync("person-id").slice(2) || undefined;
    if (!wx.getStorageSync("wx-session3rd")) {
      this.login();
    }
    this.init();
    this.checkAuthSetting();
    //获取 广告参数
    let gdt_vid = options.gdt_vid + "";
    let weixinadinfo = options.weixinadinfo;
    let aid = 0;
    if (weixinadinfo) {
      let weixinadinfoArr = weixinadinfo.split(".");
      aid = weixinadinfoArr[0];
    }
    if (
      gdt_vid != null &&
      gdt_vid != "null" &&
      gdt_vid != "undefined" &&
      gdt_vid.length > 0
    ) {
      getApp().globalData.click_id = gdt_vid;
      getApp().globalData.adinfo = aid;

      getApp().reportAD_CLICK();
    }
  },
  created() {
    this.$bus.$on("LOGIN_SUCCESS", () => {
      this.selfInfo = getApp().globalData.imInfo;
    });

    uni.getProvider({
      service: "payment",
      success: (res) => {
        this.provider = res.provider;
      },
    });
  },
  beforeDestroy() {
    this.$bus.$off("LOGIN_SUCCESS");
  },
  methods: {
    init() {
      this.getPersonDetail(); // from personMixin
      this.imLogin();
      this.checkPay();
    },
    handleSendClick() {
      this.gotoAttUpload();
    },
    imLogin() {
      var isIMLogin =
        getApp().globalData.tim && getApp().globalData.tim.getLoginUser();
      if (!isIMLogin) {
        new IMEventHandler();
      }
    },
    handleContact() {
      // 是否登录im
      var isIMLogin =
        getApp().globalData.tim && getApp().globalData.tim.getLoginUser();
      if (!isIMLogin) {
        uni.showLoading({
          title: "正在连接",
          mask: true,
        });
        let isFirst = true;
        new IMEventHandler(() => {
          if (isFirst) {
            isFirst = false;
            uni.hideLoading();
            uni.navigateTo({
              url:
                "/pagesSub/message/chat/index?isAutoSendMsg=0chatId=" +
                this.chatId,
            });
          }
        });
      } else {
        uni.navigateTo({
          url:
            "/pagesSub/message/chat/index?isAutoSendMsg=0&chatId=" +
            this.chatId,
        });
      }
    },

    // 是否已经支付
    checkPay() {
      let openId = uni.getStorageSync("openid");
      if (!openId || !this.personId) return;

      const params = {
        personId: this.personId,
        activityType: "resumeCheck",
        openId: openId,
      };
      return isPay(params).then((res) => {
        if (res.code == 200) {
          this.isPay = res.data.isPay == 1;
        }
        this.isCheck = true;
        return res;
      });
    },

    // 登录成功后处理
    handleJump(personId) {
      this.init();
      uni.showToast({
        title: "登录成功",
        icon: "none",
      });
      this.personId = personId;

      this.getPersonDetail(); // from personMixin
      this.gotoAttUpload();
      return;

      // 去支付
      this.checkPay().then((res) => {
        if (res.code == 200) {
          if (res.data.isPay != 1) {
            this.handlePay();
          }
          // else {
          //   this.handleContact();
          // };
        }
      });
    },

    // 简历上传页
    gotoAttUpload() {
      // 直接跳到简历文件上传的页面
      uni.navigateTo({
        url: `/pagesSub/career/activity/AttachmentUpload?fromWhere=${this.fromWhere}`,
      });
    },

    handlePay() {
      if (this.paying) return;
      this.paying = true;
      if (this.provider.indexOf("wxpay") >= 0) this.addWxPay();
    },
    addWxPay() {
      const openid = uni.getStorageSync("openid");
      const params = {
        personId: this.personId,
        activityType: "resumeCheck",
        openId: openid,
      };
      uni.showLoading();
      addOrderAndPay(params)
        .then((res) => {
          if (res && res.code == 200) {
            this.price = res.data.orderInfo.payAmount;
            getApp().reportCOMPLETE_ORDER(this.price); // 上报下单
            this.requestPayment(res.data.payInfo);
          } else {
            uni.hideLoading();
            uni.showToast({
              title: res.status_desc,
              icon: "none",
              duration: 2000
            });
          }
        })
        .finally(() => {
          this.paying = false;
        });
    },
    // 支付API
    requestPayment(data) {
      data.payData = JSON.parse(data.payData);
      uni.requestPayment({
        provider: this.provider[0],
        timeStamp: data.payData.timeStamp,
        nonceStr: data.payData.nonceStr,
        package: data.payData.package,
        signType: "MD5",
        paySign: data.payData.paySign,
        success: (res) => {
          this.handlePaySuccess(res);
        },
        fail: (err) => {
          this.handlePayFial(err);
        },
      });
    },
    handlePaySuccess(res) {
      getApp().reportPURCHASE(this.price); // 上报付费
      uni.hideLoading();
      this.isPay = true;
      uni.showModal({
        title: "支付成功",
        content: "现在去联系导师，导师收到消息后将在第一时间回复您！",
        confirmText: "去联系",
        confirmColor: "#f75a3e",
        success: (res) => {
          if (res.confirm) {
            this.handleContact();
          }
        },
      });
    },
    handlePayFial(err) {
      uni.hideLoading();
      this.loading = false;
      let msg = "";
      if (err.errMsg == "requestPayment:fail cancel") {
        msg = "取消支付";
      } else {
        msg = err.errMsg;
      }
      uni.showToast({
        title: msg,
        icon: "none",
      });
    },
    handleNewUser() {
      getApp().reportREGISTER();
    },
    showLoginDialog() {
      this.$refs.loginDialog.setVisibility(true);
    },
    loginSuccessBack() {
      console.log("loginSuccessBack");
      this.personId = uni.getStorageSync("person-id").slice(2) || "";
    },
  },
};
</script>

<style lang="scss" scoped>
.resume {
  position: relative;

  .diagnosis-img {
    width: 100%;
  }

  .link {
    position: fixed;
    bottom: 180rpx;
    left: 0;
    width: 100%;
  }

  .tips {
    text-align: center;
    font-size: 24rpx;
    color: #fff;
  }

  .lm-wechat {
    position: absolute;
    left: 0;
    bottom: 55rpx;
    width: 100%;
    height: 210rpx;
    ::v-deep button {
      background-color: transparent;
      padding: 0 !important;
      border: none;
    }
    ::v-deep .u-loading::after {
      background-color: transparent;
    }
  }
  .btn-confirm {
    position: fixed;
    bottom: 80rpx;
    left: 0;
    height: 210rpx;
    width: 100%;
  }
  .btn-experience {
    width: 100%;
    height: 210rpx;
  }
}
</style>
